/*
 * 软件著作权：北京康兰尚德数据科技有限公司
 * 项目名称：康兰数据购物中心分析平台
 *
 * NAME : ShopDaoImpl.java
 *
 * PURPOSE : 购物中心数据库访问
 *
 * AUTHOR : EEDATA/ywb
 *
 *
 * 创建日期: 2013-11-28
 * HISTORY：
 * 变更日期 
 */
package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.EcpGwcShopping;
import pojo.ShopSort;
import util.HibernateUtils;

/**
 * 购物中心数据访问层
 * 
 * @author ywb
 */

public class ShopDaoImpl {
	public static double DEF_PI180 = 0.01745329252; // PI/180.0
	public static double DEF_R = 6370693.5; // radius of earth

	/**
	 * 根据地址获取购物中心信息
	 */
	@SuppressWarnings("unchecked")
	public List<ShopSort> selectShop(String address, double longititue,
			double latitude) {// 七、getShoppingCenterInfoList
		Session session = null;
		Transaction ts = null;
		List<ShopSort> shopList = new ArrayList<ShopSort>();
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("from EcpGwcShopping where name like '%"
							+ address + "%' or address like '%" + address
							+ "%'");
			List<EcpGwcShopping> list = query.list();

			for (EcpGwcShopping ecpGwcShopping : list) {

				double distance = GetLongDistance(longititue, latitude,
						ecpGwcShopping.getLongitude(),
						ecpGwcShopping.getLatitude());// 单位M

				ShopSort shopSort = new ShopSort();
				shopSort.setShopid(ecpGwcShopping.getShopid());
				shopSort.setName(ecpGwcShopping.getName());
				shopSort.setLongitude(ecpGwcShopping.getLongitude());
				shopSort.setLatitude(ecpGwcShopping.getLatitude());
				shopSort.setAddress(ecpGwcShopping.getAddress());
				shopSort.setPhone(ecpGwcShopping.getPhone());
				shopSort.setOpenhour(ecpGwcShopping.getOpenhour());
				shopSort.setDistance(distance);

				shopList.add(shopSort);

			}

			// 冒泡排序
			for (int i = 0; i < shopList.size(); i++) {
				for (int j = 0; j < shopList.size(); j++) {
					ShopSort temp;
					if (shopList.get(i).getDistance() < shopList.get(j)
							.getDistance()) {
						temp = shopList.get(j);
						shopList.set(j, shopList.get(i));
						shopList.set(i, temp);
					}
				}
			}

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return shopList;

	}

	/**
	 * 获取购物中心类别
	 */
	@SuppressWarnings("unchecked")
	public List<String> selectShopDistrict() {// 十四、 getRecommendCategoryInfo接口
		Session session = null;
		Transaction ts = null;
		List<String> list = null;
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select distinct shopsort from EcpGwcShopping where shopsort!=?");
			query.setParameter(0, "");
			list = query.list();

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return list;

	}

	/**
	 * 根据购物类别获取购物中心信息
	 */
	@SuppressWarnings("unchecked")
	public List<ShopSort> selectShopBySort(String sort, double longititue,
			double latitude) {// 十四、
		// getRecommendCategoryInfo接口
		Session session = null;
		Transaction ts = null;
		List<ShopSort> shopList = new ArrayList<ShopSort>();
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("from EcpGwcShopping where shopsort=?");
			query.setParameter(0, sort);
			List<EcpGwcShopping> list = query.list();

			for (EcpGwcShopping ecpGwcShopping : list) {

				double distance = GetLongDistance(longititue, latitude,
						ecpGwcShopping.getLongitude(),
						ecpGwcShopping.getLatitude());// 单位M

				ShopSort shopSort = new ShopSort();
				shopSort.setShopid(ecpGwcShopping.getShopid());
				shopSort.setName(ecpGwcShopping.getName());
				shopSort.setLongitude(ecpGwcShopping.getLongitude());
				shopSort.setLatitude(ecpGwcShopping.getLatitude());
				shopSort.setAddress(ecpGwcShopping.getAddress());
				shopSort.setPhone(ecpGwcShopping.getPhone());
				shopSort.setOpenhour(ecpGwcShopping.getOpenhour());
				shopSort.setDistance(distance);

				shopList.add(shopSort);

			}

			// 冒泡排序
			for (int i = 0; i < shopList.size(); i++) {
				for (int j = 0; j < shopList.size(); j++) {
					ShopSort temp;
					if (shopList.get(i).getDistance() < shopList.get(j)
							.getDistance()) {
						temp = shopList.get(j);
						shopList.set(j, shopList.get(i));
						shopList.set(i, temp);
					}
				}
			}

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return shopList;

	}

	/**
	 * 根据类别id获取类别名称
	 */
	@SuppressWarnings("unchecked")
	public String selectDistrictName(String district) {// 十四、
		Session session = null;
		Transaction ts = null;
		String dicname = "";
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session
					.createQuery("select dicname from EcpDicDetail where dicid=?");
			query.setParameter(0, district);
			List<String> list = query.list();

			if (list.size() != 0)
				dicname = list.get(0);
			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return dicname;
	}

	/**
	 * 根据经纬度获取附近购物中心信息
	 */
	@SuppressWarnings("unchecked")
	public List<ShopSort> selectShopByl(double longititue, double latitude) {// 七、getShoppingCenterInfoList
		Session session = null;
		Transaction ts = null;
		List<ShopSort> shopList = new ArrayList<ShopSort>();
		try {
			session = HibernateUtils.getSession();
			ts = session.beginTransaction();

			Query query = session.createQuery("from EcpGwcShopping");
			List<EcpGwcShopping> list = query.list();
			for (EcpGwcShopping ecpGwcShopping : list) {

				double distance = GetLongDistance(longititue, latitude,
						ecpGwcShopping.getLongitude(),
						ecpGwcShopping.getLatitude());// 单位M

				ShopSort shopSort = new ShopSort();
				shopSort.setShopid(ecpGwcShopping.getShopid());
				shopSort.setName(ecpGwcShopping.getName());
				shopSort.setLongitude(ecpGwcShopping.getLongitude());
				shopSort.setLatitude(ecpGwcShopping.getLatitude());
				shopSort.setAddress(ecpGwcShopping.getAddress());
				shopSort.setPhone(ecpGwcShopping.getPhone());
				shopSort.setOpenhour(ecpGwcShopping.getOpenhour());
				shopSort.setDistance(distance);

				shopList.add(shopSort);

			}

			// 冒泡排序
			for (int i = 0; i < shopList.size(); i++) {
				for (int j = 0; j < shopList.size(); j++) {
					ShopSort temp;
					if (shopList.get(i).getDistance() < shopList.get(j)
							.getDistance()) {
						temp = shopList.get(j);
						shopList.set(j, shopList.get(i));
						shopList.set(i, temp);
					}
				}
			}

			ts.commit();

		} catch (Exception e) {
			e.printStackTrace();
			ts.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return shopList;

	}

	/*
	 * 按标准的球面大圆劣弧长度计算，适用于距离较远的情况
	 */
	private double GetLongDistance(double lon1, double lat1, double lon2,
			double lat2) {
		double ew1, ns1, ew2, ns2;
		double distance;
		// 角度转换为弧度
		ew1 = lon1 * DEF_PI180;
		ns1 = lat1 * DEF_PI180;
		ew2 = lon2 * DEF_PI180;
		ns2 = lat2 * DEF_PI180;
		// 求大圆劣弧与球心所夹的角(弧度)
		distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1)
				* Math.cos(ns2) * Math.cos(ew1 - ew2);
		// 调整到[-1..1]范围内，避免溢出
		if (distance > 1.0)
			distance = 1.0;
		else if (distance < -1.0)
			distance = -1.0;
		// 求大圆劣弧长度
		distance = DEF_R * Math.acos(distance);
		return distance;
	}

}
